﻿/*
Copyright 2011 Ben Griffith Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
*/


package {
	import flash.events.*;
	import flash.net.*;
	import flash.external.ExternalInterface;
	
	public class elq {
		var elqVer:String = "v200";
		var url:String = "http://now" + ".eloqua.com/visitor/" + elqVer + "/svrGP.aspx";
		public var siteid:String;
		public var elqData:String;
		
		public function elq(sid:String):void {
			siteid = sid;
		}
		
		public function pageTrack(page:String, referrer:String = "elqNone", callback:Function = null):void {
			var ms:uint = getTime();
			var old:Date = new Date(20020101);
			var tzo:Number = old.getTimezoneOffset();
			
			var loader:URLLoader = new URLLoader();
			var elq_request:URLRequest = new URLRequest(url + "?pps=31&siteid=" + siteid + "&ref=" + page + "&ref2=" + referrer + "&tzo=" + tzo + "&ms=" + ms);
			
			if(typeof(callback) == "function"){
				loader.addEventListener(Event.COMPLETE, callback);
			}
			
			try {
				loader.load(elq_request);
			} catch (error:Error) {
				trace("Unable to load requested document: " + error);
			}
		}
		
		public function getGUID(callback:Function = null, referrer:String = "elqNone"):void {
			var ms:uint = getTime();
			
			var loader:URLLoader = new URLLoader();
			var elq_request:URLRequest = new URLRequest(url + "?pps=70&siteid=" + siteid + "&ref=" + referrer + "&ms=" + ms);
			
			if(typeof(callback) == "function"){
				var on_complete = function(evt:Event) {
					var loader:URLLoader = URLLoader(evt.target);
					var js:String = "function(){"+trim(loader.data)+" return GetElqCustomerGUID();}";
					
					try {
						var guid = ExternalInterface.call(js);
						callback(guid);
					} catch (error:Error) {
						trace("There was an error running the Eloqua GetElqCustomerGUID javascript: " + error);
					}
					
				}
				loader.addEventListener(Event.COMPLETE, on_complete);
			}
			
			try {
				loader.load(elq_request);
			} catch (error:Error) {
				trace("Unable to load requested guid: " + error);
			}
		}
		
		public function getData(key:String, lookup:String = "", callback:Function = null):void {
			var ms:uint = getTime();
			
			var loader:URLLoader = new URLLoader();
			var elq_request:URLRequest = new URLRequest(url + "?pps=50&siteid=" + siteid + "&DLKey=" + key + "&DLLookup=" + lookup + "&ms=" + ms);
			
			var on_complete = function(evt:Event) {
				var returnLoader:URLLoader = URLLoader(evt.target);
				var js:String = loader.data;
				elqData = js;
			
				if(typeof(callback) == "function"){
					callback();
				}
			}
			loader.addEventListener(Event.COMPLETE, on_complete);
			
			try {
				loader.load(elq_request);
			} catch (error:Error) {
				trace("Unable to load requested data lookup: " + error);
			}
		}
		
		public function GetElqContentPersonalizationValue(field:String):String {
			if (elqData == "" || elqData ==  null) {
				return "data lookup must be performed, no data.";
			}
			var js:XML = 
			<script>
				<![CDATA[
					function() {
						]]>{elqData}<![CDATA[
						return GetElqContentPersonalizationValue(']]>{field}<![CDATA[');
					}
				]]> 
			</script>;
			
			var elqField;
			try {
				elqField = ExternalInterface.call(js);
			} catch (error:Error) {
				elqField = "There was an error running the Eloqua GetElqContentPersonalizationValue javascript: " + error;
			}
			return elqField;
		}
		
		public function GetElqGroupMembershipStatus(group:String):String {
			if (elqData == "" || elqData ==  null) {
				return "false";
			}
			var js:XML = 
			<script>
				<![CDATA[
					function() {
						]]>{elqData}<![CDATA[
						return GetElqGroupMembershipStatus(']]>{group}<![CDATA[');
					}
				]]> 
			</script>;
			
			var elqField;
			try {
				elqField = ExternalInterface.call(js);
			} catch (error:Error) {
				elqField = "There was an error running the Eloqua GetElqGroupMembershipStatus javascript: " + error;
			}
			return elqField;
		}
		
		public function getTime():uint {
			var now:Date = new Date();
			return now.getTime();
		}	
		
		public function trim(s:String):String {
			return s.replace(/^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2");
		}
	}
}